<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" style="margin-top: 20px;" @click="addPermissionAPI(0,1)">添加权限</el-button>
      <el-table row-key="id" border :data="list" style="margin-top: 20px;">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button v-if="row.type===1" size="mini" type="text" @click="addPermissionAPI(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="`${showText}权限点`"
      :visible.sync="showDialog"
    >
      <el-form ref="form" :model="form" :rules="rules" @close="btnCancel">
        <el-form-item label-width="20%" label="权限名称" prop="name" required>
          <el-input v-model="form.name" style="width: 80%;" />
        </el-form-item>
        <el-form-item label-width="20%" label="权限标识" prop="code" required>
          <el-input v-model="form.code" style="width: 80%;" />
        </el-form-item>
        <el-form-item label-width="20%" label="权限描述" prop="description">
          <el-input v-model="form.description" style="width: 80%;" />
        </el-form-item>
        <el-form-item label-width="20%" label="开启">
          <el-switch v-model="form.enabled" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item align="center">
          <el-button type="primary" @click="btnOK">确定</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addPermissionAPI, delPermissionAPI, getPermissionDetailAPI, getPermissionListAPI, updatePermissionAPI } from '@/api'
import { transListToTreeData } from '@/utils'

export default {
  name: 'Permission',

  data() {
    return {
      showDialog: false,
      list: [],
      form: {
        name: '',
        code: '',
        description: '',
        enabled: 0,
        type: '',
        pid: '',
        id: -1
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.form.type === 1 ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionListAPI(), 0)
    },
    addPermissionAPI(pid, type) {
      this.showDialog = true
      this.form.type = type
      this.form.pid = pid
    },
    async btnOK() {
      await this.$refs.form.validate()
      if (this.form.id > 0) {
        await updatePermissionAPI(this.form)
        this.$message.success('修改成功')
      } else {
        await addPermissionAPI(this.form)
        this.$message.success('新增成功')
      }
      this.getPermissionList()
      this.btnCancel()
    },
    btnCancel() {
      this.form = {
        name: '',
        code: '',
        description: '',
        enabled: 0,
        type: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.showDialog = false
    },
    async editPermission(id) {
      this.form = await getPermissionDetailAPI(id)
      this.showDialog = true
    },
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermissionAPI(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
}
</style>
